# Name: JoinSugarDistanceGrid.py - MCBS 10/08/2014
# Description: Joins sugarcane distances to grid and saves Tables/G1kmMiRSugRoadDist.dbf with extracted information.
# Rasters.gdb should have grid points and rasters with effective distances produced by Data/TransCost/SugarcaneDistances.py.

# import system module
import arcpy
from arcpy import env
from arcpy.sa import *

# Set workspace environment:
#env.workspace = "Rasters.gdb"
env.workspace = "C:\Users\Marcelo\Documents\Sugarcane\Offline Data\MunicBasedData\Rasters.gdb"
env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")

# Folder to store tables:
PathForTables = "C:\Users\Marcelo\Documents\Sugarcane\Offline Data\MunicBasedData\Tables"

# Input grid:
GridName            = "G1kmMiR"
# Sugarcane distance rasters:
BaseSugDistRaster   = "SugRoadDist_"
# Final grid name:
FinalGridName       = "G1kmMiRSugRoadDist"
# Name for field in table:
NameFieldTable      = "SRD"

Years = range(2004,2014)

# Create list of rasters and variables names for ExtractMultiValuesToPoints
ListToExtract = [[BaseSugDistRaster + str(Years[0]),NameFieldTable + str(Years[0])]]
for y in Years[1:len(Years)]:
	SugRaster = BaseSugDistRaster + str(y)
	NameTable = NameFieldTable + str(y)
	ListToExtract.append( [SugRaster,NameTable] )
	
arcpy.CopyFeatures_management(GridName, FinalGridName)
ExtractMultiValuesToPoints(FinalGridName,ListToExtract, "NONE")

print " Writing table ... "
TableName    = FinalGridName + ".dbf"
arcpy.TableToTable_conversion(TableName, PathForTables, TableName)
print TableName + " written."

